package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.entity.TlServiceFeeLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.job.paph.step.*;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 处理每日技术服务费
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class DealServiceFeeConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private DealServiceFeeProcessor dealServiceFeeProcessor;
    @Autowired
    private DealServiceFeeWriter dealServiceFeeWriter;
    @Autowired
    private DealServiceFeeListener dealServiceFeeListener;


    @Bean
    public Job dealServiceFeeJob() {
        return jobBuilderFactory.get("dealServiceFeeJob").incrementer(new RunIdIncrementer())
                .listener(dealServiceFeeListener)
                .start(dealServiceFeeStep())//同步前一日放款的订单
                .build();
    }

    @Bean
    public Step dealServiceFeeStep(){
        return stepBuilderFactory.get("dealServiceFeeStep")
                .<TlThirdPartyPayment, TlServiceFeeLog>chunk(anytxnBatchProperties.getDealServiceFeeConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(dealServiceFeeReader())
                .processor(dealServiceFeeProcessor)
                .writer(dealServiceFeeWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDealServiceFeeConfig().getThrottleLimit())//多线程*/
                .build();
    }



    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlThirdPartyPayment> dealServiceFeeReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new DealServiceFeeReader(sqlSessionFactory, org,anytxnBatchProperties.getDealServiceFeeConfig().getPageSize());
    }


}


